﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Xml;
using System.IO;

namespace moveErrorFile.classes
{
    class xmlHelper
    {
        public xmlHelper()
        {

        }

        public List<clsOrderConfigHeader> getConfig()
        {
            string sErrorMsg = null;
            List<clsOrderConfigHeader> configList = new List<clsOrderConfigHeader>();

            try
            {

                XmlDocument doc = new XmlDocument();

                //string sCurrentFilePath = Directory.GetCurrentDirectory() + @"\config\config.xml";
                string sCurrentFilePath = @"C:\Users\henry.woo\Desktop\moveerrorfile\moveErrorFile\bin\Debug\config\config.xml";
                clsLogHelper.writeLog(out sErrorMsg, "getConfig Path : " + sCurrentFilePath, true);
                doc.Load(sCurrentFilePath);
                string text = string.Empty;

                XmlNodeList elemListStorer = doc.GetElementsByTagName("storer");
                for (int i = 0; i < elemListStorer.Count; i++)
                {
                    string sStorer = elemListStorer[i].Attributes["value"].Value.ToString();

                    clsOrderConfigHeader configHeader = new clsOrderConfigHeader();

                    configHeader.SStorer = sStorer;

                    XmlNodeList xnStorer = doc.SelectNodes("/configuration/appSettings/storer[@value='" + sStorer + "']/OrderType");
                    

                    foreach (XmlNode xnConfigLine in xnStorer)
                    {
                        clsOrderConfigLine configLine = new clsOrderConfigLine();
                        string sOrderType = xnConfigLine.Attributes["value"].Value.ToString();


                        configLine.SCheckFolder = xnConfigLine["CheckFolder"].InnerText;
                        configLine.SOriginalDir = xnConfigLine["OriginalDir"].InnerText;
                        configLine.SSuccessDir = xnConfigLine["SuccessDir"].InnerText;
                        configLine.SErrorDir = xnConfigLine["ErrorDir"].InnerText;
                        configLine.SMoveErrorFileInd = xnConfigLine["moveErrorFilesMoveInd"].InnerText;
                        configLine.SMoveErrorFileDir = xnConfigLine["moveErrorFilesMoveDir"].InnerText;
                        configLine.IMoveErrorFileTimeFrame = Convert.ToInt32(xnConfigLine["moveErrorFilesMoveTimeFrame"].InnerText);
                        
                        configHeader.addOrderConfigLine(configLine);
                    }

                    configList.Add(configHeader);

                }
            }
            catch (Exception ex)
            {
                sErrorMsg = ex.Message;
            }

            return configList;
        }
    }
}
